package com.springboot.yuangong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.yuangong.entity.Employees;
import com.springboot.yuangong.entity.SysUser;
import com.springboot.yuangong.entity.UserRole;
import com.springboot.yuangong.entity.vo.EmployeesVo;
import com.springboot.yuangong.mapper.EmployeesMapper;
import com.springboot.yuangong.qpage.QPage;
import com.springboot.yuangong.service.IEmployeesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.yuangong.service.ISysUserService;
import com.springboot.yuangong.service.IUserRoleService;
import com.springboot.yuangong.util.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-01
 */
@Service
public class EmployeesServiceImpl extends ServiceImpl<EmployeesMapper, Employees> implements IEmployeesService {


    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IUserRoleService iUserRoleService;

    /**
     * 分页查询分类
     * @param qPage
     * @return
     */
    @Override
    public IPage getPageList(QPage qPage, EmployeesVo obj) {
        IPage iPage = new Page(qPage.getPage(), qPage.getRows());
        List<EmployeesVo> mapList = this.baseMapper.getPageList(iPage,obj);
        return iPage.setRecords(mapList);
    }

    /**
     * 新增
     * @param obj
     * @return
     */
    @Override
    public Result insert(EmployeesVo obj) {
        //验证当前账号是否存在
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",obj.getAccount());
        queryWrapper.eq("type",2);
        if(this.iSysUserService.getOne(queryWrapper)!=null){
            return Result.reError("账号已存在，请重新更换账号");
        }
        SysUser sysUser=new SysUser();
        sysUser.setAccount(obj.getAccount());
        sysUser.setPwd(obj.getPwd());
        sysUser.setName(obj.getName());
        sysUser.setCtime(new Date());
        sysUser.setType("2");
        this.iSysUserService.save(sysUser);
        Employees employees=new Employees();
        BeanUtils.copyProperties(obj,employees);
        employees.setUserid(sysUser.getUserid());
        this.save(employees);
        UserRole userRole=new UserRole();
        userRole.setRoleid(2);
        userRole.setUserid(sysUser.getUserid());
        this.iUserRoleService.save(userRole);
        return Result.reOk("恭喜，新增成功");
    }

    /**
     * 修改
     * @param obj
     * @return
     */
    @Override
    public Result updates(EmployeesVo obj) {
        Employees employees=this.getById(obj.getUserid());
        BeanUtils.copyProperties(obj,employees,new String[]{"userid"});
        this.updateById(employees);
        SysUser sysUser=this.iSysUserService.getById(obj.getUserid());
        sysUser.setPwd(obj.getPwd());
        sysUser.setName(obj.getName());
        this.iSysUserService.updateById(sysUser);
        return Result.reOk("恭喜，修改成功");
    }



}
